import * as actions from '../actions'

const userLocal = JSON.parse(window.localStorage.getItem('user'))
console.info(userLocal)
const userInitialState = {
  isLogin: userLocal? userLocal.isLogin : false,
  name: '',
  isAdmin: userLocal? userLocal.isAdmin : false
}

const user = (state = userInitialState, action) => {
  switch (action.type) {
    case actions.LOGIN_SUCCESS:
      return {
        ...state,
        isLogin: true,
        isAdmin: action.user.isAdmin,
        name: action.user.name
      }
    case actions.REGISTER_SUCCESS:
      return {
        ...state,
        name: action.user.name,
        isLogin: true,
        isAdmin: action.user.isAdmin
      }
    case actions.LOGOUT:
      return {
        ...state,
        isLogin: false
      }
    case actions.SET_USER_INFO:
      return {
        ...state,
        ...action.userInfo
      }
    default:
      return state
  }
}

export default user;